﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace W1_MenuGame
{
    /// <summary>
    /// Chịu trách nhiệm vẽ (animation) một đối tượng sprite ra màn hình
    /// </summary>
    public class My2DSprite
    {
        private List<Texture2D> _textures;

        public List<Texture2D> Textures
        {
            get { return _textures; }
            set { _textures = value; }
        }

        private int _currentTexture;

        public int CurrentTexture
        {
            get { return _currentTexture; }
            set { _currentTexture = value; }
        }

        private Vector2 _position;

        public Vector2 Position
        {
            get { return _position; }
            set { _position = value; }
        }

        private bool _isActive;

        public bool IsActive
        {
            get { return _isActive; }
            set { _isActive = value; }
        }

        private bool _isLooping;

        public bool IsLooping
        {
            get { return _isLooping; }
            set { _isLooping = value; }
        }

        private int _elapsedFrameTime;

        public int ElapsedFrameTime
        {
            get { return _elapsedFrameTime; }
            set { _elapsedFrameTime = value; }
        }

        private int _frameTime;

        public int FrameTime
        {
            get { return _frameTime; }
            set { _frameTime = value; }
        }

        private float _scale;

        public float Scale
        {
            get { return _scale; }
            set { _scale = value; }
        }

        private Color _color;   

        public Color Color
        {
            get { return _color; }
            set { _color = value; }
        }

        private Vector2 _origin;

        public Vector2 Origin
        {
            get { return _origin; }
            set { _origin = value; }
        }

        public My2DSprite()
        {
            _textures = null;
            _currentTexture = -1;
            _color = Color.White;
            _currentTexture = -1;
            _elapsedFrameTime = 50;
            _frameTime = 0;
            _isActive = false;
            _isLooping = false;
            _position = Vector2.Zero;
            _scale = 1.0f;
        }

        public void Initialize(List<Texture2D> textures, Vector2 position
            , bool isActive, bool isLooping
            , int frameTime)
        {
            _textures = textures;
            _currentTexture = 0;
            _elapsedFrameTime = 0;
            _isActive = isActive;
            _isLooping = isLooping;

            _position = position;
            _frameTime = frameTime;
            _origin = Vector2.Zero;
        }

        public void Update(GameTime gameTime)
        {
            if (!_isActive)
            {
                return;
            }

            _elapsedFrameTime += gameTime.ElapsedGameTime.Milliseconds;
            int delta = ((_elapsedFrameTime / _frameTime));
            if (delta > 0)
            {
                _currentTexture += delta;

                if (_currentTexture >= _textures.Count)
                {
                    _currentTexture = 0;

                    if (!_isLooping)
                    {
                        _isActive = false;
                    }
                }
                _elapsedFrameTime = 0;
            }

        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            //Chỉ vẽ khi sprite ở trạng thái Active
            if (_isActive)
            {
                spriteBatch.Draw(_textures[_currentTexture], _position
                    , null, _color, 0, _origin
                    , _scale, SpriteEffects.None, 0);
            }
        }
    }
}
